package com.game.jm.qyz;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.jmhy.sdk.common.ApiListenerInfo;
import com.jmhy.sdk.common.ExitListener;
import com.jmhy.sdk.common.InitListener;
import com.jmhy.sdk.common.JiMiSDK;
import com.jmhy.sdk.common.UserApiListenerInfo;
import com.jmhy.sdk.model.LoginMessageinfo;
import com.jmhy.sdk.model.PaymentInfo;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements OnClickListener {
    private final static String TAG = "JiMiSDK";

    private final int appId = 21;
    private final String appKey = "efe5a67ad0c9bdead320a19ce76cc569";

    private final List<Item> events = new ArrayList<Item>() {{
        add(new Item(0, "00 账号登录"));
        add(new Item(1, "01 账号登出"));
        add(new Item(2, "02 角色创建上报"));
        add(new Item(3, "03 角色登录上报"));
        add(new Item(4, "04 角色升级上报"));
        add(new Item(5, "05 定额充值"));
        add(new Item(6, "06 强制退出"));
    }};

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        initView();
        JiMiSDK.onCreate(this);
        JiMiSDK.initInterface(MainActivity.this, appId, appKey, new InitListener() {
            @Override
            public void Success(String s) {
                Toast.makeText(MainActivity.this, "init success", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "init Success");
            }

            @Override
            public void fail(String s) {
                Toast.makeText(MainActivity.this, "init failure", Toast.LENGTH_SHORT).show();
                Log.w(TAG, "init fail");
            }
        });

        JiMiSDK.setUserListener(new UserApiListenerInfo() {
            @Override
            public void onLogout(Object obj) {
                //账号退出成功
            }
        });

    }

    private void initView() {
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        for (Item event : events) {
            if (event.id == 5) {
                editText = new EditText(this);
                editText.setHint("请输入金额，单位分");
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_PHONE);
                editText.setText("100");
                layout.addView(editText);
                //让EditText默认不获取焦点
                layout.setFocusable(true);
                layout.setFocusableInTouchMode(true);
            }

            Button button = new Button(this);
            button.setTag(event.id);
            button.setText(event.name);
            button.setOnClickListener(this);
            layout.addView(button);
        }

        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(layout);
        setContentView(scrollView);
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch ((Integer) v.getTag()) {
            case 0:
                JiMiSDK.login(this, appId, appKey, new ApiListenerInfo() {
                    @Override
                    public void onSuccess(Object obj) {
                        LoginMessageinfo login = (LoginMessageinfo) obj;
                        if (TextUtils.equals(login.getResult(), "success")) {
                            Toast.makeText(MainActivity.this, "账号登录成功", Toast.LENGTH_SHORT).show();
                            //登录成功后显示浮标
                            JiMiSDK.showFloat();
                        } else {
                            Toast.makeText(MainActivity.this, "账号登录成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case 1:
                JiMiSDK.switchAccount(this);
                break;
            case 2:
                reportRoleInfo("1");
                break;
            case 3:
                reportRoleInfo("2");
                break;
            case 4:
                reportRoleInfo("3");
                break;
            case 5:
                PaymentInfo paymentInfo = new PaymentInfo();
                //单位分
                String amount = editText.getText().toString();
                paymentInfo.setAmount(amount);
                if (TextUtils.isEmpty(amount)) {
                    Toast.makeText(MainActivity.this, "请输入金额", Toast.LENGTH_LONG).show();
                    return;
                }
                paymentInfo.setBalance("100元宝");
                paymentInfo.setCporderid(System.currentTimeMillis() + "");
                paymentInfo.setExt(System.currentTimeMillis() + "");
                paymentInfo.setGender("男");
                paymentInfo.setLevel("99");
                paymentInfo.setOrdername("10元宝");
                paymentInfo.setPower("100000");
                paymentInfo.setRoleid("99");
                paymentInfo.setRolename("青时");
                paymentInfo.setServerno("2000");
                paymentInfo.setZoneName("青时1区");
                paymentInfo.setViplevel("12");
                JiMiSDK.payment(this, paymentInfo, new ApiListenerInfo() {
                    @Override
                    public void onSuccess(Object obj) {
                        super.onSuccess(obj);
                        if (obj != null) {
                            String str = (String) obj;
                            switch (str) {
                                case "success":
                                    Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                                    break;
                                case "fail":
                                    Toast.makeText(MainActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                                    break;
                                case "cancel":
                                    Toast.makeText(MainActivity.this, "支付取消", Toast.LENGTH_SHORT).show();
                                    break;
                            }
                        }
                    }
                });
                break;
            case 6:
                JiMiSDK.forceLogout("账号禁止登录，请联系客服人员");
                break;
        }
    }

    /**
     * 角色信息上报
     *
     * @param type 1、创建用户角色；2、进入服务器；3、玩家升级
     */
    private void reportRoleInfo(String type) {
        String roleId = "99";
        String roleName = "青时时";
        String level = "30";
        String gender = "男";
        String serverNo = "2000";
        String zoneName = "青时122区";
        String balance = "200元宝";
        String power = "116000";
        String vipLevel = "16";
        String roleCTime = System.currentTimeMillis() + "";
        String roleLevelMTime = System.currentTimeMillis() + "";
        String ext = "";
        JiMiSDK.setExtData(this, type, roleId, roleName, level, gender, serverNo, zoneName, balance, power, vipLevel, roleCTime, roleLevelMTime, ext);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            JiMiSDK.exit(this, new ExitListener() {
                @Override
                public void ExitSuccess(String msg) {
                    MainActivity.this.finish();
                }

                @Override
                public void fail(String msg) {
                    //继续游戏
                }
            });
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStart() {
        super.onStart();
//        JiMiSDK.onStart(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        JiMiSDK.onRestart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JiMiSDK.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JiMiSDK.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        JiMiSDK.onStop(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JiMiSDK.onDestroy(this);
        //注意不能在sdk的退出回调中直接杀掉进程等方式退出，不然不会走Activity的onDestroy回调
        System.exit(0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        JiMiSDK.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        JiMiSDK.onNewIntent(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        JiMiSDK.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
